package com.foreknow.view;

import com.foreknow.model.Business;
import com.foreknow.service.BusinessService;
import com.foreknow.service.impl.BusinessServiceImpl;
import com.foreknow.util.PropertyJtable;

import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JTextField;
import javax.swing.JScrollPane;

public class MainWindow extends JFrame {

    private JPanel contentPane;
    private JButton btnNewButton_1;
    private JButton btnNewButton_2;
    private JButton btnNewButton_3;
    private JButton btnNewButton_4;
    private JTextField textField;
    //    private JTable table;
    private JTable jTable;
    private Object[][] data;
    private String[] title;
    private Object[][] seldata;


    Object[][] test;
    private JTextField textField_1;

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    MainWindow frame = new MainWindow();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the frame.
     */
    public MainWindow() {
//         defaultTableModel = new DefaultTableModel();
        //单例获取对象
        PropertyJtable projtable = PropertyJtable.getJtable();
        setTitle("\u4E3B\u9875\u9762");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 827, 544);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);
        contentPane.setLayout(null);

        btnNewButton_1 = new JButton("添加");
        btnNewButton_1.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                Addinfo addinfo = new Addinfo();
                addinfo.setVisible(true);
//                MainWindow.this.dispose();
                System.out.println("添加");
            }
        });
        btnNewButton_1.setBounds(59, 26, 113, 27);
        contentPane.add(btnNewButton_1);

        btnNewButton_2 = new JButton("删除");
        btnNewButton_2.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                Delinfo delinfo = new Delinfo();
                delinfo.setVisible(true);
                MainWindow.this.dispose();

//                System.out.println("添加");
//                System.out.println("删除");
            }
        });
        btnNewButton_2.setBounds(240, 26, 113, 27);
        contentPane.add(btnNewButton_2);

        btnNewButton_3 = new JButton("修改");
        btnNewButton_3.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                UpdateById updateById=new UpdateById();
                updateById.setVisible(true);
                MainWindow.this.dispose();
                System.out.println("修改");
            }
        });
        btnNewButton_3.setBounds(404, 26, 113, 27);
        contentPane.add(btnNewButton_3);

        btnNewButton_4 = new JButton("查询");
        btnNewButton_4.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                GetQueryById getQueryById = new GetQueryById();
                getQueryById.setVisible(true);
                MainWindow.this.dispose();
                System.out.println("查询");


            }
        });
        btnNewButton_4.setBounds(581, 26, 113, 27);
        contentPane.add(btnNewButton_4);

        JButton btnNewButton = new JButton("查询(主界面)");
        btnNewButton.addActionListener(new ActionListener() {
            BusinessService businessService=new BusinessServiceImpl();
            public void actionPerformed(ActionEvent e) {
                String id1=new String(textField.getText());

                if(id1.equals("")||id1==null){
                    try {
                        List<Object> list = businessService.showAll();
                        seldata=projtable.data(list);
                    } catch (Exception exception) {
                        exception.printStackTrace();
                    }

                }else {
                    int i = Integer.parseInt(id1);
                    Object business = businessService.getQueryById(i);
//                    boolean business = businessService.del(i);
                    List<Object> idlist=new ArrayList<>();
                    idlist.add(business);
                    seldata = projtable.data(idlist);
//                   test=new Object[5][7];

                }
                projtable.update(seldata,title);
                DefaultTableModel defaultTableModel = new DefaultTableModel(seldata,title);
                jTable.setModel(defaultTableModel);

            }
        });
        btnNewButton.setBounds(498, 378, 152, 27);
        contentPane.add(btnNewButton);

        textField = new JTextField();
        textField.setBounds(102, 378, 382, 27);
        contentPane.add(textField);
        textField.setColumns(10);

        JScrollPane scrollPane = new JScrollPane();
        scrollPane.setBounds(42, 89, 652, 251);
        contentPane.add(scrollPane);




     //显示信息
        BusinessService businessService = new BusinessServiceImpl();
        try {
            List<Object> list = businessService.showAll();
            data = projtable.data(list);
        } catch (Exception e) {
            e.printStackTrace();
        }
        title = new String[]{"businessId", "password", "businessName", "Address", "Explain", "starPrice", "deliverPrice"};
//        table.setModel(new DefaultTableModel(
//                data,
//                title
//        ));

        jTable = projtable.newJtable(data, title);
        jTable.setShowGrid(false);//设置表是否绘制单元格周围的网格线。
        DefaultTableModel defaultTableModel = new DefaultTableModel(data,title);

        jTable.setModel(defaultTableModel);




//        table.getColumnModel().getColumn(0).setPreferredWidth(117);
//        table.getColumnModel().getColumn(1).setPreferredWidth(124);
//        table.getColumnModel().getColumn(2).setPreferredWidth(107);
//        table.getColumnModel().getColumn(3).setPreferredWidth(97);
//        table.getColumnModel().getColumn(4).setPreferredWidth(92);
//        table.getColumnModel().getColumn(5).setPreferredWidth(106);
//        table.getColumnModel().getColumn(6).setPreferredWidth(98);
        scrollPane.setViewportView(jTable);

        textField_1 = new JTextField();
        textField_1.setColumns(10);
        textField_1.setBounds(102, 429, 382, 27);
        contentPane.add(textField_1);

        JButton btnbusinessname = new JButton("Name\u6A21\u7CCA\u67E5\u8BE2");
        //模糊查询
        btnbusinessname.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                String name=textField_1.getText();

                BusinessService business = new BusinessServiceImpl();
                List<Object> list = business.showName(name);
                seldata=projtable.data(list);

                projtable.update(seldata,title);
                jTable.setShowGrid(false);//设置表是否绘制单元格周围的网格线。
                DefaultTableModel defaultTableModel = new DefaultTableModel(seldata,title);
                jTable.setModel(defaultTableModel);



            }
        });
        btnbusinessname.setBounds(498, 429, 152, 27);
        contentPane.add(btnbusinessname);
        //设置窗口的可见性
        this.setVisible(true);
    }
}
